Imports Clo = Enhancements.Core.Logging
Imports Cmp = System.Runtime.CompilerServices
Imports Epe = Enhancements.Basic.Primitives
Imports Ere = Enhancements.Basic.RegularExpressions
Imports Gen = System.Collections.Generic
Imports Iou = System.IO
Imports Ref = System.Reflection
Imports Reg = System.Text.RegularExpressions
Imports Sys = System
Imports Tex = System.Text
Imports Xsd = System.Xml.Schema
Imports W3c = System.Xml

Namespace Enhancements.Basic.Primitives
    ''' <summary>Contains features that add up to the .Net Framework System.DateTime primitive type.</summary>
    <Serializable(), DebuggerStepThrough()> _
    Public Module DateTime

        ''' <summary>Formats a value by using the supplied formats collection.</summary>
        ''' <param name="_value">Value to format.</param>
        ''' <param name="_formats">Formats to use.</param>
        ''' <exception cref="IsNullException">_formats is null.</exception>
        ''' <returns> </returns>
        <Cmp.Extension()> _
        Public Function Format(ByVal _value As Sys.DateTime, ByVal _formats As DateTimeFormatCollection) As String
            _formats.IsNullNot("_formats", True)

            Dim _returnValueStringBuilder As Tex.StringBuilder = New Tex.StringBuilder(Sys.String.Empty)
            For Each _format As DateTimeFormat In _formats
                _returnValueStringBuilder.Append(_format.Format(_value))
            Next _format

            Return _returnValueStringBuilder.ToString()
        End Function

        ''' <summary>Formats a value by using the supplied formats expression.</summary>
        ''' <param name="_value">Value to format.</param>
        ''' <param name="_formatsExpression">Formats expression to use.</param>
        ''' <exception cref="IsNullException">_formatsExpression is null.</exception>
        ''' <exception cref="StringInvalidException">_formatsExpression contains incomplete format expressions, or _formatsExpression contains values in between format expressions.</exception>
        ''' <exception cref="IsInRegularExpressionNotException">_formatsExpression contains one or more invalid format expressions.</exception>
        ''' <returns> </returns>
        <Cmp.Extension()> _
        Public Function Format(ByVal _value As Sys.DateTime, ByVal _formatsExpression As System.String) As String
            _formatsExpression.IsNullNot("_formatsExpression", True)
            Dim _formatsExpressionGroups() As System.String = _formatsExpression.SplitGroups(DateTimeFormat.___ExpressionPrefix, DateTimeFormat.___ExpressionSuffix, True)
            For Each _formatsExpressionGroup As System.String In _formatsExpressionGroups
                _formatsExpressionGroup.IsInRegularExpression("_formatsExpressionGroup", New System.String() {DateTimeFormatDay.___RegularExpression, DateTimeFormatHour.___RegularExpression, DateTimeFormatMillisecond.___RegularExpression, DateTimeFormatMinute.___RegularExpression, DateTimeFormatMonth.___RegularExpression, DateTimeFormatMonthName.___RegularExpression, DateTimeFormatSecond.___RegularExpression, DateTimeFormatSeparator.___RegularExpression, DateTimeFormatText.___RegularExpression, DateTimeFormatWeekday.___RegularExpression, DateTimeFormatWeekdayName.___RegularExpression, DateTimeFormatYear.___RegularExpression}, True)
            Next _formatsExpressionGroup

            Dim _formats As DateTimeFormatCollection = New DateTimeFormatCollection()
            For Each _formatsExpressionGroup As System.String In _formatsExpressionGroups
                Dim _dateTimeFormatDay As DateTimeFormatDay = Nothing
                Dim _dateTimeFormatHour As DateTimeFormatHour = Nothing
                Dim _dateTimeFormatMillisecond As DateTimeFormatMillisecond = Nothing
                Dim _dateTimeFormatMinute As DateTimeFormatMinute = Nothing
                Dim _dateTimeFormatMonth As DateTimeFormatMonth = Nothing
                Dim _dateTimeFormatMonthName As DateTimeFormatMonthName = Nothing
                Dim _dateTimeFormatSecond As DateTimeFormatSecond = Nothing
                Dim _dateTimeFormatSeparator As DateTimeFormatSeparator = Nothing
                Dim _DateTimeFormatText As DateTimeFormatText = Nothing
                Dim _dateTimeFormatWeekday As DateTimeFormatWeekday = Nothing
                Dim _dateTimeFormatWeekdayName As DateTimeFormatWeekdayName = Nothing
                Dim _dateTimeFormatYear As DateTimeFormatYear = Nothing
                If DateTimeFormatDay.TryParse(_formatsExpressionGroup, _dateTimeFormatDay) Then
                    _formats.Add(_dateTimeFormatDay)
                Else
                    If DateTimeFormatHour.TryParse(_formatsExpressionGroup, _dateTimeFormatHour) Then
                        _formats.Add(_dateTimeFormatDay)
                    Else
                        If DateTimeFormatMillisecond.TryParse(_formatsExpressionGroup, _dateTimeFormatMillisecond) Then
                            _formats.Add(_dateTimeFormatMillisecond)
                        Else
                            If DateTimeFormatMinute.TryParse(_formatsExpressionGroup, _dateTimeFormatMinute) Then
                                _formats.Add(_dateTimeFormatMinute)
                            Else
                                If DateTimeFormatMonth.TryParse(_formatsExpressionGroup, _dateTimeFormatMonth) Then
                                    _formats.Add(_dateTimeFormatMonth)
                                Else
                                    If DateTimeFormatMonthName.TryParse(_formatsExpressionGroup, _dateTimeFormatMonthName) Then
                                        _formats.Add(_dateTimeFormatMonthName)
                                    Else
                                        If DateTimeFormatSecond.TryParse(_formatsExpressionGroup, _dateTimeFormatSecond) Then
                                            _formats.Add(_dateTimeFormatSecond)
                                        Else
                                            If DateTimeFormatSeparator.TryParse(_formatsExpressionGroup, _dateTimeFormatSeparator) Then
                                                _formats.Add(_dateTimeFormatSeparator)
                                            Else
                                                If DateTimeFormatText.TryParse(_formatsExpressionGroup, _DateTimeFormatText) Then
                                                    _formats.Add(_DateTimeFormatText)
                                                Else
                                                    If DateTimeFormatWeekday.TryParse(_formatsExpressionGroup, _dateTimeFormatWeekday) Then
                                                        _formats.Add(_dateTimeFormatWeekday)
                                                    Else
                                                        If DateTimeFormatWeekdayName.TryParse(_formatsExpressionGroup, _dateTimeFormatWeekdayName) Then
                                                            _formats.Add(_dateTimeFormatWeekdayName)
                                                        Else
                                                            If DateTimeFormatYear.TryParse(_formatsExpressionGroup, _dateTimeFormatYear) Then
                                                                _formats.Add(_dateTimeFormatYear)
                                                            End If
                                                        End If
                                                    End If
                                                End If
                                            End If
                                        End If
                                    End If
                                End If
                            End If
                        End If
                    End If
                End If
            Next _formatsExpressionGroup
            Dim _returnValue As System.String = _value.Format(_formats)

            Return _returnValue
        End Function

    End Module
End Namespace
